package com.nondo.dean.platform.controller.api.admin;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.nondo.dean.common.annotation.ApiVersion;
import com.nondo.dean.common.tool.BeanUtils;
import com.nondo.dean.common.tool.ValidateUtil;
import com.nondo.dean.common.web.Page;
import com.nondo.dean.common.web.Query;
import com.nondo.dean.common.web.View;
import com.nondo.dean.platform.domain.City;
import com.nondo.dean.platform.model.AreaBean;
import com.nondo.dean.platform.service.CityService;

/**
 * 城市管理后台接口
 * @author zlj
 * @date 2018年3月27日
 */
@RestController("apiAdminCity")
@RequestMapping("/{v}/admin/city")
public class CityController {
    
    @Autowired
    CityService cityService;
    
    /**
     * 查看城市详情
     * @param request
     * @param response
     * @param id
     * @return
     */
    @ApiVersion(1)
    @RequestMapping("/info/{id}")
    public View<City> info(HttpServletRequest request, HttpServletResponse response, @PathVariable("id") Integer id) {
        View<City> view = new View<City>(20000, "获取数据成功");
        
        City city = null;
        if (id != null) {
            city = cityService.selectCity(id);
        } else {
            city = new City();
        }

        view.setData(city);
        return view;
    }
    
    /**
     * 添加城市
     * @param request
     * @param response
     * @param city 城市信息 {@link City}
     * @return
     */
    @ApiVersion(1)
    @RequestMapping("/add")
    public View<City> add(HttpServletRequest request, HttpServletResponse response,
            @Validated @RequestBody City city, BindingResult result) {
        View<City> view = new View<City>(20000, "添加成功");

        if (result.hasErrors()) {
            view.setCode(40000);
            view.setMessage(ValidateUtil.first(result.getAllErrors()));
        } else {
            City cityDb = this.cityService.selectCityByName(city.getName());
            if (cityDb != null) {
                return View.of500Error("城市名称已存在");
            }
            this.cityService.addCity(city);
        }
        return view;
    }

    /**
     * 更新城市
     * @param request
     * @param response
     * @param city 城市信息 {@link City}
     * @return
     */
    @ApiVersion(1)
    @RequestMapping("/update")
    public View<City> update(HttpServletRequest request,
            HttpServletResponse response,
            @Validated @RequestBody City city, BindingResult result) {
        View<City> view = new View<City>(20000, "更新成功");

        if (result.hasErrors()) {
            view.setCode(40000);
            view.setMessage(ValidateUtil.first(result.getAllErrors()));
        } else {
            City cityDb = cityService.selectCity(city.getId());
            if (cityDb.getName().equals(city.getName()) && cityDb.getId() != city.getId()) {
                return View.of500Error("城市名称已存在");
            }
            BeanUtils.copy(city, cityDb);
            this.cityService.updateCity(cityDb);
        }

        return view;
    }

    /**
     * 获取城市分页查询列表
     * @param request
     * @param response
     * @param query
     * @return
     */
    @ApiVersion(1)
    @RequestMapping("/list")
    public View<Page<City>> list(HttpServletRequest request, HttpServletResponse response, @RequestBody Query<City> query) {
        View<Page<City>> view = new View<Page<City>>(20000, "获取列表成功");

        City city = query.getData();

        Page<City> page = new Page<City>(query.getPage(), query.getLimit(), cityService.count(city));
        
        List<City> cityList = cityService.getCityList(query.getOffset(), query.getLimit(), city);
        
        page.setContent(cityList);
        
        view.setData(page);
        
        return view;
    }

    /**
     * 删除城市
     * @param request
     * @param response
     * @param id
     * @return
     */
    @ApiVersion(1)
    @SuppressWarnings("rawtypes")
    @RequestMapping("/delete/{id}")
    public View delete(HttpServletRequest request, HttpServletResponse response, @PathVariable("id") Integer id) {
        View view = new View(20000, "删除成功");

        City city = cityService.selectCity(id);
        city.setDelFlag(String.valueOf(1));
        this.cityService.updateCity(city);

        return view;
    }
    
    /**
     * 按照省份获取城市列表
     * @param request
     * @param response
     * @param code 省份代码
     * @return
     */
    @ApiVersion(1)
    @RequestMapping("/list/{code}")
    public View<List<AreaBean>> list(HttpServletRequest request, HttpServletResponse response, @PathVariable("code") String code) {
        View<List<AreaBean>> view = new View<List<AreaBean>>(20000, "获取列表成功");

        List<AreaBean> cityList = cityService.getCityListByProviceCode(code);
        
        view.setData(cityList);
        
        return view;
    }
}